package com.qyhl.shop.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.qyhl.shop.R;
import com.qyhl.shop.common.ShopConfig;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.ToastUtils;

import razerdp.basepopup.BasePopupWindow;

/**
 * 支付弹框
 */
public class PayPopupWindow extends BasePopupWindow {
    private View popupView;
    private RadioGroup payTypeGroup;
    private TextView payPriceTxt;
    private TextView commitBtn;
    private RadioButton wechatPayBtn;
    private RadioButton alipayBtn;

    private PayTypeInterface payTypeInterface;

    private int payType;

    public PayPopupWindow(Activity context, double price) {
        super(context);
        bindEvent();
        init(context, price);
    }


    public void setPayTypeInterface(PayTypeInterface payTypeInterface) {
        this.payTypeInterface = payTypeInterface;
    }

    /**
     * 初始化
     */
    private void init(Context context, double price) {
        refreshPrice(price);

        payTypeGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.pay_wechat_rBtn) {
                    payType = ShopConfig.PAY_TYPE_WECHAT;
                } else if (checkedId == R.id.pay_alipay_rBtn) {
                    payType = ShopConfig.PAY_TYPE_ALIPAY;
                }
            }
        });

        commitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (payType == ShopConfig.PAY_TYPE_WECHAT) {
                    ToastUtils.showToast(context, "暂不支持微信支付");
                } else if (payType == ShopConfig.PAY_TYPE_ALIPAY) {
                    payTypeInterface.payType(payType);
                    dismiss();
                } else {
                    ToastUtils.showToast(context, "请选择支付方式");
                }
            }
        });
    }

    private void bindEvent() {
        payTypeGroup = popupView.findViewById(R.id.pay_type_group);

        payPriceTxt = popupView.findViewById(R.id.pay_price_txt);

        commitBtn = popupView.findViewById(R.id.pay_commit_btn);

        wechatPayBtn = popupView.findViewById(R.id.pay_wechat_rBtn);

        alipayBtn = popupView.findViewById(R.id.pay_alipay_rBtn);

        wechatPayBtn.setVisibility(View.GONE);

        //默认支付宝
        alipayBtn.setChecked(true);
        payType = ShopConfig.PAY_TYPE_ALIPAY;
    }

    @Override
    protected Animation initShowAnimation() {
        return getDefaultScaleAnimation();
    }

    @Override
    public View getClickToDismissView() {
        return null;
    }

    @Override
    public View onCreatePopupView() {
        popupView = LayoutInflater.from(getContext()).inflate(R.layout.shop_popup_window_pay, null);
        return popupView;
    }

    @Override
    public View initAnimaView() {
        return findViewById(R.id.popup_anima);
    }

    public interface PayTypeInterface {
        void payType(int type);
    }

    @SuppressLint("SetTextI18n")
    public void refreshPrice(double price) {
        payPriceTxt.setText("¥" + StringUtils.priceTranslate(price));
    }
}
